package com.sinovatio.tls.zttbg.webPage.controller.service;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class TestService {
    public String getPaginatiorData(Integer pageNum, Integer pageSize) {
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(getJson()).getAsJsonArray();
        int start = (pageNum - 1) * pageSize;
        int end = start + pageSize;

        JsonArray arr = new JsonArray();

        for(int i=0, size=jsonArray.size(); i<size; i++) {
            if (i >= start && i<end) {
                arr.add(jsonArray.get(i));
            }
         }

        Map<String, Object> res = new HashedMap();
        res.put("total", jsonArray.size());
        res.put("arr", arr);

        return parser.parse(new Gson().toJson(res)).getAsJsonObject().toString();
    }

    private String getJson() {
        String json = "[" +
                "{name:1,age:2,sex:3}," +
                "{name:q,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:a,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:s,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:ff,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}," +
                "{name:1,age:2,sex:3}" +
                "]";

        return json;
    }
}
